package gui;
import functions.CreateTestsDS;
import functions.GenerateProgram;

import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import types.Block;


public class CreateDS {

	private static JTextField txtProgramSize;
	private static JTextField txtIF;
	private static JTextField txtIF_ELSE;
	private static JTextField txtFor;
	private static JTextField txtWhile;
	private static JTextField txtFunc;
	private static Block[] blocks;
	//private static int code_line_num;
	
	
	/**
	 * @wbp.parser.entryPoint
	 */
	public static JPanel CreateDsPanel()
    {
		
     // panel1
        final JPanel panel = new JPanel();
        final JScrollPane scrollPane = new JScrollPane();
        scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
        	public void adjustmentValueChanged(AdjustmentEvent e) {
                scrollPane.repaint();
            }
        });
        scrollPane.setViewportBorder(null);
        
        JLabel lblProgramSize = new JLabel("Program size:");
        
        txtProgramSize = new JTextField();
        txtProgramSize.setText("1000");
        txtProgramSize.setColumns(10);
        
        txtIF = new JTextField();
        txtIF.setText("15");
        txtIF.setColumns(10);
        
        final JCheckBox chckbxIF = new JCheckBox("IF");
        
        final JCheckBox chckbxIF_ELSE = new JCheckBox("IF-ELSE");
        
        txtIF_ELSE = new JTextField();
        txtIF_ELSE.setText("10");
        txtIF_ELSE.setColumns(10);
        
        final JCheckBox chckbxFor = new JCheckBox("For");
        
        txtFor = new JTextField();
        txtFor.setText("15");
        txtFor.setColumns(10);
        
        final JCheckBox chckbxWhile = new JCheckBox("While");
        
        txtWhile = new JTextField();
        txtWhile.setText("5");
        txtWhile.setColumns(10);
        
        final JCheckBox chckbxFunc = new JCheckBox("Functions");
        
        txtFunc = new JTextField();
        txtFunc.setText("5");
        txtFunc.setColumns(10);
        
        final JCheckBox chckbxException = new JCheckBox("Include exceptions");
        chckbxException.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        
        final JButton btnGenerate = new JButton("Generate Program");
        final JButton btnGenTests = new JButton("Generate Tests");
        
        btnGenerate.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {

        		if(chckbxIF.isSelected() == false)				txtIF.setText("0");
        		if(chckbxIF_ELSE.isSelected() == false)			txtIF_ELSE.setText("0");
        		if(chckbxFor.isSelected() == false)				txtFor.setText("0");
        		if(chckbxWhile.isSelected() == false)			txtWhile.setText("0");
        		if(chckbxFunc.isSelected() == false)			txtFunc.setText("0");
        		
        		if(txtProgramSize.getText().equals("")){	
        			JOptionPane.showMessageDialog(null,"num of program size cant be null","Input error", JOptionPane.ERROR_MESSAGE);
        			return;}
        		if(txtIF.getText().equals("")){
        			JOptionPane.showMessageDialog(null,"num of IF cant be null","Input error", JOptionPane.ERROR_MESSAGE);
        			return;}
        		if(txtIF_ELSE.getText().equals("")){
        			JOptionPane.showMessageDialog(null,"num of IF_ELSE cant be null","Input error", JOptionPane.ERROR_MESSAGE);
        			return;}
        		if(txtFor.getText().equals("")){
        			JOptionPane.showMessageDialog(null,"num of For cant be null","Input error", JOptionPane.ERROR_MESSAGE);
        			return;}
        		if(txtWhile.getText().equals("")){
        			JOptionPane.showMessageDialog(null,"num of While cant be null","Input error", JOptionPane.ERROR_MESSAGE);
        			return;}
        		if(txtFunc.getText().equals("")){
        			JOptionPane.showMessageDialog(null,"num of Functions cant be null","Input error", JOptionPane.ERROR_MESSAGE);
        			return;}
	
        		blocks = GenerateProgram.getGenerateProgram(txtProgramSize.getText(), txtIF.getText(), txtIF_ELSE.getText(), txtFor.getText(), txtWhile.getText(), txtFunc.getText());
        			
        		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        	    scrollPane.setViewportBorder(new LineBorder(Color.BLACK));
        	    scrollPane.setViewportView(ProgramBlocksWindow.getAllBlocksPanel(blocks));
        	    
        	    scrollPane.setVisible(true);
        	    btnGenTests.setEnabled(true);
              	}
        });
        
        JLabel lblCreateDataSet = new JLabel("Create Data Set");
        lblCreateDataSet.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
        
        JButton btnBack = new JButton("Back");
        btnBack.addActionListener(new ActionListener() {
        	@SuppressWarnings("static-access")
			public void actionPerformed(ActionEvent e) {
        		MainMenu mmPanel = new MainMenu();
        		panel.removeAll();
        		panel.add(mmPanel.getMainPanel());
        		panel.validate();
        		panel.repaint();
        	}
        });
        
        
        btnGenTests.setEnabled(false);
        btnGenTests.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		btnGenerate.setEnabled(false);
        		new CreateTestsDS(blocks, Integer.parseInt(txtProgramSize.getText()), chckbxException.isSelected());		
        	}
        });
        

        

       
         
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
        	gl_panel.createParallelGroup(Alignment.TRAILING)
        		.addGroup(gl_panel.createSequentialGroup()
        			.addContainerGap(35, Short.MAX_VALUE)
        			.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        				.addComponent(btnGenTests, GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
        				.addComponent(chckbxException)
        				.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
        					.addComponent(btnGenerate, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        					.addGroup(Alignment.LEADING, gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        							.addComponent(chckbxIF, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addGap(5)
        							.addComponent(txtIF, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
        						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        							.addComponent(lblProgramSize, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addGap(5)
        							.addComponent(txtProgramSize, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
        						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        							.addComponent(chckbxIF_ELSE, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addGap(5)
        							.addComponent(txtIF_ELSE, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
        						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        							.addComponent(chckbxFor, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addGap(5)
        							.addComponent(txtFor, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
        						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        							.addComponent(chckbxWhile, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addGap(5)
        							.addComponent(txtWhile, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
        						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        							.addComponent(chckbxFunc, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addGap(5)
        							.addComponent(txtFunc, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)))))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        				.addComponent(lblCreateDataSet)
        				.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 460, GroupLayout.PREFERRED_SIZE))
        			.addGap(14))
        		.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
        			.addGap(16)
        			.addComponent(btnBack)
        			.addContainerGap(609, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
        	gl_panel.createParallelGroup(Alignment.TRAILING)
        		.addGroup(gl_panel.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(lblCreateDataSet)
        			.addGap(18)
        			.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
        					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 377, GroupLayout.PREFERRED_SIZE)
        					.addGap(28))
        				.addGroup(gl_panel.createSequentialGroup()
        					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_panel.createSequentialGroup()
        							.addGap(6)
        							.addComponent(lblProgramSize))
        						.addComponent(txtProgramSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_panel.createSequentialGroup()
        							.addGap(2)
        							.addComponent(chckbxIF))
        						.addComponent(txtIF, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_panel.createSequentialGroup()
        							.addGap(2)
        							.addComponent(chckbxIF_ELSE))
        						.addComponent(txtIF_ELSE, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_panel.createSequentialGroup()
        							.addGap(2)
        							.addComponent(chckbxFor))
        						.addComponent(txtFor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_panel.createSequentialGroup()
        							.addGap(2)
        							.addComponent(chckbxWhile))
        						.addComponent(txtWhile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_panel.createSequentialGroup()
        							.addGap(2)
        							.addComponent(chckbxFunc))
        						.addComponent(txtFunc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addComponent(btnGenerate, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
        					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        					.addComponent(chckbxException)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(btnGenTests)
        					.addGap(36)))
        			.addGap(3)
        			.addComponent(btnBack)
        			.addGap(14))
        );
        
        panel.setLayout(gl_panel);
        
        // title
        JLabel lblClassificationOfRegression = new JLabel("");
        lblClassificationOfRegression.setIcon(new ImageIcon(RunCrst.class.getResource("/images/CRST_bg.png")));
        lblClassificationOfRegression.setHorizontalAlignment(SwingConstants.CENTER);
        lblClassificationOfRegression.setFont(new Font("Tahoma", Font.BOLD, 22));
    
        panel.setSize(700, 500);
        return panel;
    }
}
